//
//  DULogin.m
//  APP
//
//  Created by duyujin1 on 2021/8/24.
//

#import "DULogin.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "DUListItem.h"
#import "DUAlert.h"
#import "DUDetailViewController.h"

@interface DULogin()<TencentSessionDelegate>

@property (nonatomic, strong, readwrite) TencentOAuth *oauth;
@property (nonatomic, copy, readwrite) DULoginFinishBlock finishBlock;
@property (nonatomic, assign, readwrite) BOOL isLogin;

@end

@implementation DULogin

+(instancetype)sharedLogin{
    static DULogin *login;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        login=[[DULogin alloc] init];
    });
    return login;
}

-(instancetype)init{
    if(self=[super init]){
        _isLogin=NO;
        _oauth = [[TencentOAuth alloc] initWithAppId:@"222222" andDelegate:self];
    }
    return self;
}

-(BOOL)isLogin{
    //判断登陆态是否失效的逻辑
    return _isLogin;
}

-(void)loginWithFinishBlock:(DULoginFinishBlock)finishBlock{
    _finishBlock = [finishBlock copy];
    
    _oauth.authMode = kAuthModeClientSideToken;
    [_oauth authorize:@[kOPEN_PERMISSION_GET_USER_INFO,
                        kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
                        kOPEN_PERMISSION_ADD_ALBUM,
                        kOPEN_PERMISSION_ADD_ONE_BLOG,
                        kOPEN_PERMISSION_ADD_SHARE,
                        kOPEN_PERMISSION_ADD_TOPIC,
                        kOPEN_PERMISSION_CHECK_PAGE_FANS,
                        kOPEN_PERMISSION_GET_INFO,
                        kOPEN_PERMISSION_GET_OTHER_INFO,
                        kOPEN_PERMISSION_LIST_ALBUM,
                        kOPEN_PERMISSION_UPLOAD_PIC,
                        kOPEN_PERMISSION_GET_VIP_INFO,
                        kOPEN_PERMISSION_GET_VIP_RICH_INFO]];
    
}

-(void)logOut{
    [_oauth logout:self];
    _isLogin = NO;
}

#pragma mark - delegate

- (void)tencentDidLogin {
    _isLogin = YES;
    //保存openid
    //确认登录之后请求用户信息
    [_oauth getUserInfo];
}

- (void)tencentDidNotLogin:(BOOL)cancelled {
    if (_finishBlock) {
        _finishBlock(NO);
    }
}

- (void)tencentDidNotNetWork {

}

- (void)tencentDidLogout {
   //退出登录，需要清理下存储在本地的登录数据
}

- (void)getUserInfoResponse:(APIResponse *)response {
    NSDictionary *userInfo = response.jsonResponse;
    _nick = userInfo[@"nickname"];
    _address = userInfo[@"province"];
    _avatarUrl = userInfo[@"figureurl_qq_2"];
    if (_finishBlock) {
        _finishBlock(YES);
    }
}

#pragma mark- 分享
-(void)shareToQQWithArticleUrl:(NSURL *)articleUrl formViewController:(UIViewController *)vc{
    //登录校验
    if (![[DULogin sharedLogin] isLogin]) {
        [DUAlert showAlertWithTitle:@"是否登录？" detailIfon:@"登录后可使用分享功能" confirmHandle:^{
            [[DULogin sharedLogin] loginWithFinishBlock:^(BOOL isLogin) {
                //SDK流程之后判断是否登录成功
                if (isLogin) {
                    // 通知  刷新'我的'页面
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshMineView" object:nil];
                    //登录成功提示
                    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"登录成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
                    [DUDetailViewController dismiss:alert afterDelay:1.0 formViewController:vc];
                }
            }];
        } cancelHandle:^{
            //
        }];
    }
    else{
        DUListItem * item=[[DUListItem alloc] init];
        QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:articleUrl title:item.title description:item.url previewImageURL:nil];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
        __unused QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    }
}

@end
